package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.DataMeta;
import com.etone.smartAudit.dto.dataMeta.DataMetaDTO;
import com.etone.smartAudit.error.CustomParameterizedException;
import com.etone.smartAudit.mapper.DataMetaMapper;
import com.etone.smartAudit.mapstruct.DataMetaMapstruct;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.dataMeta.DataMetaVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: sovy
 * @date: 2020/11/18
 * @description:
 */

@Service
@AllArgsConstructor
public class DataMetaService extends ServiceImpl<DataMetaMapper, DataMeta> {

    private DataMetaMapstruct dataMetaMapstruct;

    /**
     * 数据字典列表
     *
     * @param dataModeId
     * @param keys
     * @param page
     * @param pageSize
     * @return
     */
    public PagerVO<DataMetaVO> list(Integer dataModeId, String keys, int page, int pageSize) {
        Page<DataMetaVO> dataMetaVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> baseMapper.findList(dataModeId, keys));
        return PagerVO.of(dataMetaVOPage);
    }


    /**
     * 更新数据字典
     *
     * @param dataModelId
     * @param dataMetaDTOS
     */
    @Transactional(rollbackFor = Exception.class)
    public List<DataMeta> save(Integer dataModelId, List<DataMetaDTO> dataMetaDTOS) {
        if (dataModelId != null) {
            deleteByDateModelId(dataModelId);
        }
        if (!CollectionUtils.isEmpty(dataMetaDTOS)) {

            //判断是否有重复性
            Map<String, Long> nameSizeMap = dataMetaDTOS.stream().collect(Collectors.groupingBy(DataMetaDTO::getFieldName, Collectors.counting()));
            String repeatNames = nameSizeMap.keySet().stream().filter(key -> nameSizeMap.get(key) > 1).collect(Collectors.joining(","));
            if (!StringUtils.isEmpty(repeatNames)) {
                throw new CustomParameterizedException(repeatNames + "字段存在重复项");
            }

            List<DataMeta> dataMetas = dataMetaMapstruct.getDataMeta(dataMetaDTOS);
            dataMetas.forEach(dataMeta -> {
                dataMeta.setModelId(dataModelId);
            });
            saveOrUpdateBatch(dataMetas);
            return dataMetas;
        }
        return null;
    }

    /**
     * 根据模型id删除元数据
     *
     * @param dataModelId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByDateModelId(Integer dataModelId) {
        QueryWrapper queryWrapper = new QueryWrapper(DataMeta.builder().modelId(dataModelId).build());
        baseMapper.delete(queryWrapper);
    }
}
